import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flashapp/widgets/loginBtn.dart';
import 'package:flashapp/About.dart';
import 'package:http2/transport.dart';

class Hello extends StatelessWidget{
  String result = '数据对象';

   void _getData() async {
//    Dio dio = new Dio();
//    Response<Map> response = await dio.get("http://server.58yilu.com/coin/coin");
//    result = response.data['body'].toString();
//
//    print(result);


     var uri = Uri.parse("https://http2.golang.org/clockstream");

     var socket = await connect(uri);

     var transport = new ClientTransportConnection.viaSocket(socket);

     var headers = [
       new Header.ascii(':method', 'GET'),
       new Header.ascii(':path', uri.path),
       new Header.ascii(':scheme', uri.scheme),
       new Header.ascii(':authority', uri.host),
     ];

     var stream = transport.makeRequest(headers, endStream: true);
     await for (var message in stream.incomingMessages) {
       if (message is HeadersStreamMessage) {
         for (var header in message.headers) {
           var name = utf8.decode(header.name);
           var value = utf8.decode(header.value);
           print('headersStreamMessage-----------');
           print('$name: $value');
         }
       } else if (message is DataStreamMessage) {
//         print('dataStreamMessage---------');
         print(utf8.decode(message.bytes));
       }
     }
     await transport.finish();
   }

  Future<Socket> connect(Uri uri) async {
    bool useSSL = uri.scheme == 'https';
    if (useSSL) {
      var secureSocket = await SecureSocket.connect(
          uri.host, uri.port, supportedProtocols: ['h2']);
      if (secureSocket.selectedProtocol != 'h2') {
        throw new Exception(
            "Failed to negogiate http/2 via alpn. Maybe server "
                "doesn't support http/2.");
      }
      print(secureSocket);
      return secureSocket;
    } else {
      return await Socket.connect(uri.host, uri.port);
    }
  }
  

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    _getData();
    return Scaffold(
      appBar: AppBar(
        title: Text('Hello 页面'),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text('关于我们'),
            new RaisedButton(
                child: new Text('点击跳转到关于我们'),
                onPressed: (){
                  Navigator.push(context,
                      new MaterialPageRoute(builder: (context){
                        return new About();
                      })
                  );
                }
            ),
            new LoginBtn()
          ],
        ),
      )
    );
  }
}